# Copyright 1999-2013 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

EAPI=4

inherit eutils multilib git-2

DESCRIPTION="libretro implementation of bnes. (Nintendo Entertainment System)"
HOMEPAGE="http://gitorious.org/bsnes/bsnes"
SRC_URI=""

EGIT_REPO_URI="git://gitorious.org/bsnes/bsnes.git"
EGIT_BRANCH="libretro"

LICENSE="GPL-2"
SLOT="0"
KEYWORDS="~x86 ~amd64 ~arm"
IUSE="bsnes_accuracy +bsnes_balanced bsnes_performance"

REQUIRED_USE="^^ ( bsnes_accuracy bsnes_balanced bsnes_performance )"

RDEPEND=""

DEPEND=""

S="${WORKDIR}/higan"

src_unpack() {
	S="${WORKDIR}" git-2_src_unpack
}

src_prepare() {
	epatch "${FILESDIR}"/${PN}-makefile.patch

	sed -i "target-libretro/Makefile" \
		-e "s:lib/:$(get_libdir)/:g" \
		|| die
}

src_compile() {
	mkdir bsnes
	emake profile=performance \
		ui=target-libretro \
                compiler=gcc \
		|| die "emake failed"
	mv ./out/bsnes_libretro.so ./bsnes/bsnes_performance_libretro.so
	use bsnes_performance && ln -s ./bsnes/bsnes_performance_libretro.so ./bsnes_libretro.so

	emake clean || die
        emake profile=balanced \
                ui=target-libretro \
                compiler=gcc \
                || die "emake failed"
	mv ./out/bsnes_libretro.so ./bsnes/bsnes_balanced_libretro.so
	use bsnes_balanced && ln -s ./bsnes/bsnes_balanced_libretro.so ./bsnes_libretro.so

	emake clean || die
        emake profile=accuracy \
                ui=target-libretro \
                compiler=gcc \
                || die "emake failed"
	mv ./out/bsnes_libretro.so ./bsnes/bsnes_accuracy_libretro.so
	use bsnes_accuracy && ln -s ./bsnes/bsnes_accuracy_libretro.so ./bsnes_libretro.so
}



src_install() {
        exeinto /usr/$(get_libdir)/libretro/bsnes
        doexe ./bsnes/*.so || die
        insinto /usr/$(get_libdir)/libretro
        doins ./bsnes_libretro.so || die
}

pkg_postinst() {
	elog "All profiles are installed to \"/usr/$(get_libdir)/libretro/bsnes\""
	if use bsnes_accuracy; then
		elog "\"/usr/$(get_libdir)/libretro/bsnes_libretro.so\" was symlinked to the accuracy profile."
	fi
	if use bsnes_balanced; then
		elog "\"/usr/$(get_libdir)/libretro/bsnes_libretro.so\" was symlinked to the balanced profile."
	fi
	if use bsnes_performance; then
		elog "\"/usr/$(get_libdir)/libretro/bsnes_libretro.so\" was symlinked to the performance profile."
	fi
	elog "You can always change the symlink to you favorite profile."
}